// package1/pages/person/pub.js
import service from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sn: '',
    phone: '',
    shareLisr: [],
    userInfo: {
      // nick: 'yang'
    },
    visible: false,
    show: false,
    uid : '',
    gunId: ''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('options',options)
    this.setData({
      gunId: options.sn
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList()
  },
    //共享列表
    getList(){
      let that = this
      service("POST","/personal/gun/share/list", {gunId: this.data.sn}).then(res => {
       if(res.data.code ==0){
         this.setData({
           shareList : res.data.page.list
         })
       }
      })
    },

    //搜索用户
    searchUser(){
      let that = this
      let phone = this.data.phone
      let error
      if(phone.length <11){
        error = '请输入完整的手机号'
        wx.showToast({
          title: error,
          icon: 'none'
        })
        return
      }else if(phone.length > 11){
        error = '手机号不正确'
        wx.showToast({
          title: error,
          icon: 'none'
        })
        return
      }
      service("POST","/user/search", phone,'text').then(res => {
        this.setData({
          visible: true
        })
       if(res.data.code ==0){
         if(res.data.users.length>0){
          this.setData({
            userInfo : res.data.users[0]
          })
         }
        
       }
      })
    },
    setPhone(e){
      console.log('setphone', e)
      let phone = e.detail.value
      this.setData({
        phone : phone
      })
      // if(phone.length >11){
       
      // }
    },
    onCancel(){
      this.setData({
        visible: false
      })
    },
    showConfir(e){
      this.setData({
        uid: e.currentTarget.dataset.id,
        show: true
      })
    },
    showCancel(){
      this.setData({
        show: false,
        userId: ''
      })
    },
    delUser(){
      let that = this
      let gunId = this.data.gunId
      let uid = this.data.uid
      service("POST","/personal/gun/share/del", {gunId: this.data.gunId, uid: this.data.uid}).then(res => {
        this.setData({
          uid: ''
        })
        if(res.data.code ==0){
          that.setData({
            show: false,
          })
          wx.showToast({
            title: '操作成功',
            icon: 'success'
          })
          
          this.getList()
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
       })
    },
    addUser(){
      let that = this
      service("POST","/personal/gun/share/add", {gunId: this.data.gunId, uid: this.data.userInfo.id}).then(res => {
       if(res.data.code ==0){
         that.getList()
         this.setData({
           visible : false
         })
         wx.showToast({
           title: '添加成功',
         })
       }else{
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
       }
      })
    },





  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})